<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--<%@taglib prefix="wetechfn" uri="/WEB-INF/tlds/wetech-functions.tld" %>--%>
<%@taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
<%@ taglib prefix="shiro" uri="/WEB-INF/tlds/shiros.tld" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<!-- Content Header (Page header) -->
<section class="content-header" style="">
    <h1>
        风控审核
        <small>风控审核页面</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
        <li><a href="#">风控审核</a></li>
        <li class="active">风控审核</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <%--<ul class="nav nav-tabs">
            <li class="active"><a href="#" onclick="loadHref('#approval/test')">单表列表</a></li>
            <li><a href="#approval/test">单表添加</a></li>
        </ul>--%>
        <div class="col-xs-12">
            <div class="box">
                <!-- /.box-header -->
                <div class="box-body">
                    <p id="toolbar">
                    <form class="form-inline">
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-addon">名称</div>
                                <input type="text" class="form-control" name="comName" id="comName_id"
                                       placeholder="关键字...">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <label class="control-label">审核状态</label>
                                <div class="radio-inline">
                                    <label>
                                        &nbsp;&nbsp;
                                        <input type="radio"  name="status_id" id="status_id" value="0">
                                        全部
                                    </label>
                                </div>
                                <div class="radio-inline">
                                    <label>
                                        <input type="radio"  name="status_id" id="status_id" checked="checked" value="1">
                                        未审
                                    </label>
                                </div>
                                <div class="radio-inline">
                                    <label>
                                        <input type="radio"  name="status_id" id="status_id"  value="2">
                                        已审
                                    </label>
                                </div>
                            </div>
                        </div>
                        <button type="button" class="btn btn-primary queryButton" onclick='return false;'>查询</button>
                    </form>
                    <table id="table"></table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>

    </div>
    <!-- /.row -->
</section>
<!-- /.content -->

<!-- 风控审核通过 -->
<div class="modal fade" id="passModal" tabindex="-1" role="dialog" aria-labelledby="passModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="addModalLabel">审核通过</h4>
            </div>
            <div class="modal-body">
                <form id="passForm" enctype="multipart/form-data">
                    <input type="hidden" id="compay_id" name="id" readonly required>
                    <input type="hidden" id="status" name="operateType" value="5" readonly required>
                    <div class="form-group">
                        <label class="control-label"><span class="asterisk">*</span>评审意见</label>
                        <textarea autofocus id="com_name" class="form-control" name="remark" placeholder="评审意见" minlength="1" required/>
                        <div class="help-block with-errors"></div>
                    </div>

                    <div class="box box-danger">
                        <div class="box-header with-border">
                            <h3 class="box-title">上传证件</h3>
                        </div>
                        <div class="box-body">
                            <div class="form-group">
                                <div class="col-sm-4">
                                    <label for="exampleInputFile1">风控评审意见(通过)扫描件</label>
                                    <input type="file" id="exampleInputFile1" class="imagelimit">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary" id="passButton"  data-dismiss="modal">确定</button>
            </div>
            <div id="J_progress_label" style="display: none;position: absolute;width: 200px;height: 60px;text-align: center;line-height: 60px;top: 0;left: 0;right: 0;bottom: 0;border-radius:15px;margin: auto;box-shadow:0px 0px 5px #e5f9f2;background:#f63;z-index:999;color:white;">

            </div>
        </div>
    </div>
</div>

<!-- 风控审核拒绝 -->
<div class="modal fade" id="noPassModal" tabindex="-1" role="dialog" aria-labelledby="noPassSmallModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="noPassSmallModalLabel">审核驳回</h4>
            </div>
            <div class="modal-body">
                <form id="noPassForm" enctype="multipart/form-data">
                    <input type="hidden" id="no_id" name="id" readonly required>
                    <input type="hidden" id="no_status" name="operateType" value="6" readonly required>
                    <div class="form-group">
                        <label class="control-label"><span class="asterisk">*</span>评审意见</label>
                        <textarea autofocus  class="form-control" name="remark" id="noPassRemark" placeholder="评审意见" minlength="1" required/>
                        <div class="help-block with-errors"></div>
                    </div>

                    <div class="box box-danger">
                        <div class="box-header with-border">
                            <h3 class="box-title">上传证件</h3>
                        </div>
                        <div class="box-body">
                            <div class="form-group">
                                <div class="col-sm-4">
                                    <label for="exampleInputFile12">风控评审意见(拒绝)扫描件</label>
                                    <input type="file" id="exampleInputFile12" class="imagelimit">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="noPassButton" data-dismiss="modal">确定</button>
            </div>
            <div id="J_progress_label" style="display: none;position: absolute;width: 200px;height: 60px;text-align: center;line-height: 60px;top: 0;left: 0;right: 0;bottom: 0;border-radius:15px;margin: auto;box-shadow:0px 0px 5px #e5f9f2;background:#f63;z-index:999;color:white;">

            </div>
        </div>
    </div>
</div>

<!-- msgShow modal -->
<div class="modal fade" id="msgShowModal" tabindex="-1" role="dialog" aria-labelledby="msgShowSmallModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="msgSmallModalLabel">驳回原因</h4>
            </div>
            <div class="modal-body">
                <div id="showMsgDiv"></div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script>

    function loadHref(s) {
        $.myAdmin.loadContent(s);
    }

    //查询
    $(document).on('click', ".queryButton",function(){
        $('#table').bootstrapTable('refresh');
    });

    var $table = $('#table');

    $(function () {
        // bootstrap table初始化
        // http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/
        $table.bootstrapTable({
            url: path + '/riskCheck/queryByStatusRiskCheck',
            idField: 'id',
            searchOnEnterKey: false,
            showColumns: false,
            detailView: false,
            //得到查询的参数
            queryParams : function (params) {
                //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                var temp = {
                    limit: params.limit,                         //页面大小
                    offset: params.offset,   //页码
                    // sort: params.sort,      //排序列名
                    order: params.order, //排位命令（desc，asc）
                    comName: $("#comName_id").val(),
                    riskStatus: $("input[name='status_id']:checked").val()
                };
                return temp;
            },
            columns: [
                {field: 'id', title: '编号'},
                {field: 'comName', title: '名称'},
                {
                    field: 'comType', title: '类别', formatter: function (value, row, index) {
                        if (value == 1) {
                            return "核心企业"
                        } else if (value == 2) {
                            return "一般企业"
                        } else if (value == 3) {
                            return "个人"
                        }
                    }
                },
                {field: 'businessTypeSTR', title: '业务品种'},
                 /*{field: 'creditLine', title: '授信额度'},*/
                {field: 'inputTime', title: '录入时间'},
                {field: 'activeTime', title: '激活时间'},
                {field: 'upTime', title: '上线时间'},
                {
                    field: 'status', title: '所处阶段', formatter: function (value, row, index) {
                    if (value == 1) {
                        if(row.msgCount>0){
                            return "<a href='javascript:;' onclick='showMsgDetail("+row.id+")'>待尽调<sup style='color: red'>"+row.msgCount+"</sup></a>";
                        }else{
                            return "待尽调"
                        }
                    } else if (value == 2) {
                        if (row.msgCount > 0) {
                            return "<a href='javascript:;' onclick='showMsgDetail(" + row.id + ")'>待部门审核<sup style='color: red'>" + row.msgCount + "</sup></a>";
                        } else {
                            return "待部门审核"
                        }
                    } else if (value == 3) {
                        if(row.msgCount>0){
                            return "<a href='javascript:;' onclick='showMsgDetail("+row.id+")'>待风控审核<sup style='color: red'>"+row.msgCount+"</sup></a>";
                        }else{
                            return "待风控审核"
                        }
                    } else if (value == 4) {
                            return "待上线"
                        } else if (value == 5) {
                            return "贷后管理"
                        } else if (value == 6) {
                            return "存档"
                        }
                    }
                },
                {field: 'passTime', title: '通过时间'},
                /*{field: 'supportId', title: '担保方信息'},*/
                {
                    field: 'action',
                    title: '操作',
                    halign: 'center',
                    align: 'center',
                    formatter: 'actionFormatter',
                    events: 'actionEvents',
                    clickToSelect: false
                }
            ]
        });
    });
    //参数传递
    function Search() {
        var temp = {
            datetime: $("#nowdate").val()
        };
        return temp;
    }

    // 数据表格展开内容
    function detailFormatter(index, row) {
        var html = [];
        $.each(row, function (key, value) {
            html.push('<p><b>' + key + ':</b> ' + value + '</p>');
        });
        return html.join('');
    }
    function actionFormatter(value, row, index) {
        str= '\
        <button id="detail_id" class="btn btn-info" type="button" >资料审核</button>　\
        ';
        if(row.status==3){
            str+='\
        <button id="pass" class="btn btn-success" type="button" >通过</button>　\
        <button id="noPass" class="btn btn-danger" type="button">驳回</button>　\
        ';
        }

        return str;
    }

    //非空校验
    function checkInput(selectI) {

        var checkFlag = true;

        if(selectI=="1"){
            if ($("#com_name").val() == "") {
                alert("评审意见不能为空");
                checkFlag = false;
                return checkFlag;
            }
            if ($("#exampleInputFile1").val() == "") {
                alert("风控评审意见扫描件不能为空");
                checkFlag = false;
                return checkFlag;
            }
        }else{
            if ($("#noPassRemark").val() == "") {
                alert("评审意见不能为空");
                checkFlag = false;
                return checkFlag;
            }
            if ($("#exampleInputFile12").val() == "") {
                alert("风控评审意见扫描件不能为空");
                checkFlag = false;
                return checkFlag;
            }
        }
        return checkFlag;
    }

    //风控审核拒绝
    $("#noPassButton").click(function () {
            if(checkInput("2")) {
                var sizeflag = true;
                $("#noPassButton").attr("disabled", "disabled");
                var id = $("#no_id").val();
                var operateType = $("#no_status").val();
                var remark = $("#noPassRemark").val();
                var formData = new FormData();
                var sizeTotal=0;
                var strParms = "id=" + id + "&operateType=" + operateType + "&remark=" + remark;
                $("#noPassForm :input").each(function () {
                    var type = $(this).attr("type");
                    if (type == "file" && $(this).attr("style") != "display:none") {
                        var name = $(this).attr("id");
                        var label_name = "";
                        var filename = $(this)[0].files[0];
                        $('label[for="' + name + '"]').each(function () {
                            if ($(this).attr("style") != "display:none") {
                                label_name = $(this).text();
                            }
                        });
                        if (filename == null) {
                            strParms = strParms + "&" + encodeURI(encodeURI(label_name)) + "=";
                        } else {
                            formData.append(label_name, filename);
                            sizeTotal=sizeTotal+filename.size;
                        }
                    }
                });

                if(sizeTotal>200*1024*1024){
                    sizeflag=false;
                    alert("文件需小于200M");
                    $("#noPassButton").removeAttr("disabled");
                    return;
                }
                if(sizeflag) {
                    var that = this;
                    var loadnum = 0;
                    var oldloaded = 0;
                    $.ajax({
                        type: 'post',
                        url: '/riskCheck/checkStatus?' + strParms,
                        cache: false,
                        data: formData,
                        contentType: false,
                        processData: false,
                        xhr: function () {
                            myXhr = $.ajaxSettings.xhr();
                            if (myXhr.upload) {
                                $(that).parents('.modal ').find('#J_progress_label').show();
                                myXhr.upload.addEventListener('progress', function (e) {
                                    if (e.lengthComputable) {
                                        if (e.loaded != 0) {
                                            loadnum += Math.abs(e.loaded - oldloaded);
                                            console.log(e.loaded, loadnum, e.total);
                                            oldloaded = e.loaded;
                                        } else {
                                            oldloaded = 0;
                                        }

                                        var percent = Math.floor(loadnum / e.total * 100);
                                        if (percent <= 100) {
                                            $(that).parents('.modal ').find("#J_progress_label").html('已上传：' + percent + '%');
                                        }
                                        if (percent >= 100) {
                                            $(that).parents('.modal ').find("#J_progress_label").html('文件上传完毕，请等待处理...');
                                        }
                                    }
                                }, false);
                            }
                            return myXhr;
                        },
                        success: function (data) {
                            $.myNotify.success(data.msg);
                            $("#noPassModal").modal('hide');
                            loadHref('#riskCheck');
                            $("div").removeClass("modal-backdrop");
                        },
                        error: function (data) {
                            $.myNotify.danger(data.msg);
                            $("#noPassButton").removeAttr("disabled");
                        }
                    })
                }
            }
    })

    //风控审核通过
    $("#passButton").click(function () {
        if(checkInput("1")){
        $("#passButton").attr("disabled", "disabled");
        var id = $("#compay_id").val();
        var operateType=$("#status").val();
        var remark =$("#com_name").val();
        var formData = new FormData();
        var sizeTotal=0;
            var sizeflag = true;
        var strParms = "id=" + id+"&operateType="+operateType+"&remark="+remark;
        $("#passForm :input").each(function () {
            var type = $(this).attr("type");
            if (type == "file" && $(this).attr("style") != "display:none") {
                var name = $(this).attr("id");
                var label_name = "";
                var filename = $(this)[0].files[0];
                $('label[for="' + name + '"]').each(function () {
                    if ($(this).attr("style") != "display:none") {
                        label_name = $(this).text();
                    }
                });
                if (filename == null) {
                    strParms = strParms + "&" + encodeURI(encodeURI(label_name)) + "=";
                } else {
                    formData.append(label_name, filename);
                    sizeTotal=sizeTotal+filename.size;
                }
            }
        });
            if(sizeTotal>200*1024*1024){
                sizeflag = false;
                alert("文件需小于200M");
                $("#passButton").removeAttr("disabled");
                return;
            }

            if(sizeflag) {
                var that = this;
                var loadnum = 0;
                var oldloaded = 0;
                $.ajax({
                    type: 'post',
                    url: '/riskCheck/checkStatus?' + strParms,
                    cache: false,
                    data: formData,
                    contentType: false,
                    processData: false,
                    xhr: function () {
                        myXhr = $.ajaxSettings.xhr();
                        if (myXhr.upload) {
                            $(that).parents('.modal ').find('#J_progress_label').show();
                            myXhr.upload.addEventListener('progress', function (e) {
                                if (e.lengthComputable) {
                                    if (e.loaded != 0) {
                                        loadnum += Math.abs(e.loaded - oldloaded);
                                        console.log(e.loaded, loadnum, e.total);
                                        oldloaded = e.loaded;
                                    } else {
                                        oldloaded = 0;
                                    }

                                    var percent = Math.floor(loadnum / e.total * 100);
                                    if (percent <= 100) {
                                        $(that).parents('.modal ').find("#J_progress_label").html('已上传：' + percent + '%');
                                    }
                                    if (percent >= 100) {
                                        $(that).parents('.modal ').find("#J_progress_label").html('文件上传完毕，请等待处理...');
                                    }
                                }
                            }, false);
                        }
                        return myXhr;
                    },
                    success: function (data) {
                        $.myNotify.success(data.msg);
                        $("#passModal").modal('hide');
                        loadHref('#riskCheck');
                        $("div").removeClass("modal-backdrop");
                    },
                    error: function (data) {
                        $.myNotify.danger(data.msg);
                        $("#passButton").removeAttr("disabled");
                        //$("#passModal").modal('hide');
                        //loadHref('#riskCheck');
                    }
                })
            }
        }
    })

    window.actionEvents = {
        'click #pass': function (e, value, row, index) {
            $('#passModal').modal('show');
            $('#compay_id').val(row.id)
        },
        'click #noPass': function (e, value, row, index) {
            $('#noPassModal').modal('show');
            $('#no_id').val(row.id)
        },
        'click #detail_id': function (e, value, row, index) {
            var id = row.id
            $.myAdmin.loadContent('#riskCheck/detail?id='+id+'');
        }
    };

    var setting = {
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };

    function showMsgDetail(row){
        $.ajax({
            type : "post",
            url : "/archives/approvalRecordList?id="+row,
            async : false,
            success : function(data){
                if (data.status != 0){
                    alert(data.msg);
                }else {
                    $("#msgShowModal").modal('show');
                    var list = [];
                    list = data.msglist;
                    listlength = list.length;
                    var detailHtml = '<tr><th style="width:200px;">原因</th><th style="width:260px;">时间</th></tr>';
                    for (var i = 0; i < list.length; i++) {
                        var demo = list[i].demo;
                        var operateTime = list[i].operateTime;
                        if(operateTime==""||operateTime==null||operateTime=="null"){
                            operateTime="-";
                        }
                        detailHtml += '<tr>';
                        detailHtml += '<td class="txt_c" style="width:200px">'+ demo +'</td>';
                        detailHtml += '<td class="c_blue txt_c" style="width:260px">'+ operateTime +'</td>';
                        detailHtml += '</tr>';
                    }
                    $("#showMsgDiv").html(detailHtml);
                }
            }
        });
    }

    $(".imagelimit").change(function(){
        var file = $(this)[0].files[0];
        if(file !=null && file!=undefined && file !="") {
            var name=file.name.toLowerCase();
            if (name.indexOf(".png") > 0 ||name.indexOf(".jpg") > 0 ||name.indexOf(".pdf") > 0 || name.indexOf(".rar") > 0 || name.indexOf(".zip") > 0 || name.indexOf(".jpeg") > 0 || name.indexOf(".gif") > 0 || name.indexOf(".bmp") > 0) {
            } else {
                alert("文件类型不支持");
                $(this).val("");
            }
        }
    })

</script>